package com.egao.common.manage.school;

import cn.hutool.core.util.StrUtil;
import com.egao.common.core.annotation.ApiPageParam;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.student.entity.vo.StudentSignStatusVO;
import com.egao.common.module.student.service.StudentService;
import com.egao.common.module.system.constants.DictConstants;
import com.egao.common.module.system.constants.UserConstants;
import com.egao.common.module.system.entity.Dict;
import com.egao.common.module.system.entity.User;
import com.egao.common.module.system.service.CollegeService;
import com.egao.common.module.system.service.DictService;
import com.egao.common.module.system.service.MajorService;
import com.egao.common.module.system.service.QsSchoolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学校就业状态管理
 * @author lym
 */
@Api(tags = "学校就业状态管理")
@RestController
@RequestMapping("${api}")
public class SchoolSignStatusController extends BaseController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private MajorService majorService;
    @Autowired
    private DictService dictService;
    @Autowired
    private QsSchoolService qsSchoolService;

    private final String title = "学校就业状态管理";

    @PreAuthorize("hasAuthority('school:status:list')")
    @OperLog(value = title, desc = "分页查询就业状态")
    @ApiOperation(value = "分页查询就业状态")
    @ApiPageParam
    @GetMapping("/school/status/page")
    public PageResult<StudentSignStatusVO> page(HttpServletRequest request) {
        User loginUser = getLoginUser();

        PageParam<StudentSignStatusVO> pageParam = new PageParam<>(request);

        return new PageResult<>(studentService.listSignStatusPage(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('school:status:list')")
    @ApiOperation(value = "查询学院")
    @GetMapping("/school/status/college")
    public JsonResult colleges() {
        return JsonResult.ok().setData(collegeService.listAll(null, ""));
    }

    @PreAuthorize("hasAuthority('school:status:list')")
    @ApiOperation(value = "查询专业")
    @GetMapping("/school/status/majors")
    public JsonResult majors() {
        return JsonResult.ok().setData(majorService.listAll(null, null));
    }

    @PreAuthorize("hasAuthority('school:status:list')")
    @ApiOperation(value = "查询就业状态字典")
    @GetMapping("/school/status/dict")
    public JsonResult dicts() {
        Map<String, List<Dict>> map = new HashMap<>();
        map.put("signStatus", dictService.cacheList(DictConstants.SIGN_STATUS));
        map.put("companyIndustry", dictService.cacheList(DictConstants.COMPANY_INDUSTRY));
        map.put("dcLqyxdm", dictService.cacheList(DictConstants.TYPE_DC_LQYXDM));
        return JsonResult.ok().setData(map);
    }

    @PreAuthorize("hasAuthority('college:status:list')")
    @ApiOperation(value = "洲国家学校级联数据")
    @GetMapping("/school/status/qs")
    public JsonResult qs() {
        return qsSchoolService.treeAll();
    }

    @PreAuthorize("hasAuthority('school:status:list')")
    @OperLog(value = title, desc = "更新学生就业状态", result = true)
    @ApiOperation(value = "更新学生就业状态")
    @PutMapping("/school/status/update")
    public JsonResult update(@RequestBody StudentSignStatusVO studentSignStatusVO) {
        if (StrUtil.isBlank(studentSignStatusVO.getSignStatusLabel())) {
            return JsonResult.error("请选择就业状态");
        }

        return studentService.updateStatus(studentSignStatusVO, getLoginUserId());
    }

    @PreAuthorize("hasAuthority('school:status:list')")
    @OperLog(value = title, desc = "导出")
    @ApiOperation(value = "导出")
    @GetMapping("/school/status/export")
    public void export(HttpServletRequest request, HttpServletResponse response) {
        PageParam<StudentSignStatusVO> pageParam = new PageParam<>(request);
        // 取全部设置一个一定超出的长度
        pageParam.setSize(20000);
        List<StudentSignStatusVO> list = studentService.listSignStatusPage(pageParam);
        studentService.exportStatusExcel(list, response);
    }
}
